/**
 * 获取投影为单位的半径
 * @param {ol/map} map  openlayer的地图实例
 * @param {Number} radius 以米为单位的半径的值
 * @returns {Number} circleRadius 以投影为单位的半径的值
 */
const getProjectionRadius = function(map, radius) {
  if(radius <= 0) {
    return 0
  }
  const metersPerUnit = map.getView().getProjection().getMetersPerUnit()
  let circleRadius = radius / metersPerUnit
  return circleRadius
}

/**
 * 获取米为单位的半径
 * @param {ol/map} map  openlayer的地图实例
 * @param {*} radius 以投影为单位的半径的值
 * @returns {Number} circleRadius 以米为单位的半径的值
 */
const getMeterRadius = function(map, radius) {
  if(radius <= 0) {
    return 0
  }
  const metersPerUnit = map.getView().getProjection().getMetersPerUnit()
  let circleRadius = radius * metersPerUnit
  return circleRadius
}

const formatLength = function(length) {
  let output = ''
  if(length > 100) {
    output = Math.round((length / 1000) * 100) / 100 + ' ' + 'km'
  }else {
    output = Math.round(length * 100) / 100 + ' ' + 'm';
  }
  return output
}

module.exports = {
  getProjectionRadius,
  getMeterRadius,
  formatLength
}